<?php 
ob_start(); 
?>
<?php
session_start();
require 'openid.php';
require 'conexion.php'; ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">

<html>
<head>
    <title>Validacion OpenId</title>
    <meta http-equiv=Content-Type content="text/html; charset=ISO-8859-1">
</head>	
<? try {
    $openid = new LightOpenID;
    if(!$openid->mode) {
    
            $openid->identity = 'https://www.google.com/accounts/o8/id?openid.mode=checkid_immediate';
			$openid->required = array('namePerson/first', 'namePerson/last', 'contact/email');
            header('Location: ' . $openid->authUrl());
     
    } elseif($openid->mode == 'cancel') {
        echo 'Usuario ha cancelado la autenticacion!';
    } else {
       // echo 'Usuario ' . ($openid->validate() ? $openid->identity . ' has ' : 'no ha') . 'iniciado sesion.';
		//echo 'Usuario <b>' . $openid->identity . '</b> se ha logueado.<br>';

			//echo "<h3>Informacion</h3>";

			$identity = $openid->identity;
			$attributes = $openid->getAttributes();
			$email = $attributes['contact/email'];
			$first_name = $attributes['namePerson/first'];
			$last_name = $attributes['namePerson/last'];

			/*echo "modo: " . $openid->mode . "<br>";
			echo "identidad: " . $identity . "<br>";
			echo "correo: " . $email . "<br>";
			echo "primer nombre: " . $first_name . "<br>";
			echo "segundo nombre: " . $last_name . "<br>";*/
			//echo "aqui nosotros tenemos el control";
			$sql="select * from clientes where id_correo='$email'";
			$datos=mysql_query($sql);
			$total=mysql_num_rows($datos);
			$fila=mysql_fetch_assoc($datos);
			if ($total==0)
			{
			  $insert="INSERT INTO clientes (id_correo, nombre1,apellido1,fecha_nacimiento) VALUES ('$email','$first_name','$last_name','0000-00-00')";
			  mysql_query($insert);
			  $primeravez=0;
			  $id=mysql_insert_id();
			  
			}
			else
			{
			  $id=$fila["id"];
			  $primeravez=$fila["primeravez"];
			  }
			$_SESSION['usuario']=$first_name." ".$last_name;
			$_SESSION['correo']=$email;
			$_SESSION['idusuario']=$id;
               $rurl = "http://localhost/envios";
              //header("Location:$rurl");   
			   //document.location="http://localhost/envios/index.php?id=<?=$_SESSION['usuario']; 
			  if ($primeravez==0) {  ?>
			  <script>
			  //document.location="http://localhost/envioscake/index.php?iduser=<?=$_SESSION['usuario'];?>&idmail=<?=$_SESSION['correo'];?>&idnumber=<?=$id;?>";
			  document.location="http://localhost/envioscake/app/webroot/panel/pages/modificarcliente.php";
			  </script>
   <? }
     else
	   { ?>
	     <script>
			  document.location="http://localhost/envioscake/index.php?iduser=<?=$_SESSION['usuario'];?>&idmail=<?=$_SESSION['correo'];?>&idnumber=<?=$id;?>";
			  
			  </script>
	   <? }
} } catch(ErrorException $e) {
    echo $e->getMessage();
}
?>
</html>
<?php 
ob_end_flush(); 
?>